#include "spacecol2d.h"

#define BPR2D 7

SpaceCol2D::SpaceCol2D()
{
    BPR = BPR2D;
    BPRSQ = BPR*BPR;
    numBlock = BPR*BPR;
    m_blocks = new Block[numBlock];
    m_tree = new TreeGen2D();
    ((TreeGen2D *)m_tree)->genTree();
    m_widget = NULL;
    blockSize = WIDTH2D/BPR;
    //m_renderer = NULL;
    //numBuds = 0;

}

SpaceCol2D::~SpaceCol2D()
{
     //if (m_tree) m_tree->freeTree(m_tree->treeRoot);
    // delete[] m_blocks;
    // m_blocks = NULL;

}

void SpaceCol2D::updateTree(TreeGen2D *newTree)
{
    if (m_tree) m_tree->freeTree(m_tree->treeRoot);
    m_tree = newTree;
}

void SpaceCol2D::clearTree(){
    delete m_tree;
    m_tree = new TreeGen2D();
   ( (TreeGen2D *)m_tree)->genTree();

    delete[] m_blocks;

}

// generate markers
void SpaceCol2D::generateMarkers()
{
    double size = blockSize;
    /**
    for (int i = 0; i < numBlock; i++)
    {
        Block * currentBlock = &m_blocks[i];
        currentBlock->col = (int) i % BPR;
        currentBlock->row = (int) i / BPR;
        currentBlock->updateSize(size, float3 (currentBlock->col * size, currentBlock->row * size, 0));
        for (int j = 0; j < currentBlock->numMarkers; j++)
        {
            double random = ((double) rand()) / (double) RAND_MAX;
            double range = size; (currentBlock->col + 1) * size - currentBlock->startCoord.x;
            currentBlock->m_markers.push_back( (random*range) + currentBlock->startCoord.x); // x coord

            double random2 = ((double) rand()) / (double) RAND_MAX;
            double range2 = size; (currentBlock->row + 1) * size - currentBlock->startCoord.y;
            currentBlock->m_markers.push_back((random2*range2) + currentBlock->startCoord.y); // y coord

            currentBlock->m_markers.push_back( 0); // z coord, will be used in 3D;
            currentBlock->m_markers.push_back( -1); // does not belong to any bud;
            currentBlock->m_markers.push_back( -1); // no distance calculated yet;
            if (currentBlock->m_markers.at(j*5 + 0) < 0 || currentBlock->m_markers.at(j*5 + 1) < 0)
            {
                cout << "smaller than 0:" << currentBlock->col << "//" << currentBlock->row << endl;
               // cout << "value:" << currentBlock->m_markers[j*5 + 0] << "//" << currentBlock->m_markers[j*5 + 1] << endl;
                cout << "random:" << random << "//" << random2 << endl;
                cout << "range:" << range << "//" << range2 << endl;
                cout << "startCoord:" << currentBlock->startCoord << endl;
                cout << "size:" << size << endl;
                cout << endl;
            }
        }
    }*/
}

// draw markers, for debugging purpose.
void SpaceCol2D::drawMarkers()
{
    //GLUquadric * mQuadric = gluNewQuadric();
    for (int k = 0; k < numBlock; k++)
    {
        Block * currentBlock = &m_blocks[k];
        for (int j = 0; j < currentBlock->m_markers.size()/5.0; j++)
        {
            if(currentBlock->m_markers.at(j*5   +3)> -1.9 && currentBlock->m_markers.at(j*5 +4)>-1.9)
            {
                if (currentBlock->m_markers.at(j*5   +3) > 0 && currentBlock->m_markers.at(j*5   +4) > 0)
                    glColor3f(1.0, 1.0, 0.0);
                else
                    glColor3f(0.0, 0.0, 1.0);
                glPushMatrix();
                glTranslatef(currentBlock->m_markers.at(j*5 + 0), currentBlock->m_markers.at(j*5 + 1), 0.0);
                glBegin(GL_QUADS);
                    glVertex3f(-.5,-.5,0);
                    glVertex3f(.5,-.5,0);
                    glVertex3f(.5,.5,0);
                    glVertex3f(-.5,.5,0);
                glEnd();
                //gluDisk(mQuadric, 0, 2.0/2.0, 10, 10);
                glPopMatrix();
            }

        }
    }
}

void SpaceCol2D::addMarkers(double x, double y, double z)
{
    //is this right??
    double size = blockSize;
    int col = (int)x/size;
    int row = (int)y/size;
    int index = row*BPR + col;
   /* if(x>500.0 || y>500.0  || z>500.0){
        cout<<"indexing too high, returning"<<endl;
        return;
    }
    else if(x<0 || y<0 || z<0){
        cout<<"indexing too low, returning"<<endl;
        return;
    }*/
    m_blocks[index].m_markers.push_back(x);
    m_blocks[index].m_markers.push_back(y);
    m_blocks[index].m_markers.push_back(z);
    m_blocks[index].m_markers.push_back(-1);
    m_blocks[index].m_markers.push_back(-1);
}

// create a tree root
void SpaceCol2D::initialize(float3 start, float3 end, float theta, float diameter)
{
    m_tree = new TreeGen2D();
    m_tree->treeRoot->start = start;
    m_tree->treeRoot->end = end;
    m_tree->treeRoot->theta = theta;
    m_tree->treeRoot->diameter = diameter;
    m_tree->treeRoot->aux = NULL;
    m_tree->treeRoot->term = NULL;
    m_tree->treeRoot->direction = float3(0,1.0,0);
    numBuds ++;
    m_tree->treeRoot->spaceColIndex = numBuds;
    //internodeLength = diameter;
    //m_tree->calculateBranchDiameter(m_tree->treeRoot);
}
